package pro.lepers.portfolio.core {
	import fr.digitas.flowearth.bi_internal;
	import fr.digitas.flowearth.command.Batcher;
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.mvc.address.structs.INode;
	import fr.digitas.flowearth.mvc.address.structs.Node;
	import fr.digitas.flowearth.mvc.address.structs.connector.SWFAddressConnector;
	import fr.digitas.flowearth.mvc.address.structs.descriptor.BaseDescriptor;
	import fr.digitas.flowearth.mvc.address.structs.system.nodeSystem;
	import fr.digitas.flowearth.text.styles.styleManager;

	import pro.lepers.portfolio.text.FontLoader;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.URLRequest;

	/**
	 * @author plepers
	 */
	public class Bootstrap extends EventDispatcher {
		

		private function coreInit () : void {
			
			_runStyles();
			_runNodes( );
			_runSwfAddress( );
		}

		private function _runStyles () : void {
			styleManager.addCss( Conf.styles );
		}

		private function _runNodes () : void {
			var descriptor : BaseDescriptor = new BaseDescriptor( Conf.getDatas( "tree" ).node[0] );
			var rootNode : INode = new Node( descriptor );
			nodeSystem.addDevice( rootNode );
		}

		private function _runSwfAddress () : void {
			swfAddressConnector.connectNode( nodeSystem.getDefaultDevice() );
			swfAddressConnector.connectAddress();
		}


		
		
		
		
		
		
		
		
		public function Bootstrap (target : IEventDispatcher = null) {
			super( target );
			
			_batcher = new Batcher( );
			_batcher.addEventListener(Event.COMPLETE, onComplete );
		}

		public function run () : void {
			_batcher.addItem( Conf.bi_internal::getBatcher() );
			Conf.loadXml( new URLRequest( Conf.confFile ) );
			
			_batcher.addItem( new FontLoader( new QName( "fonts" ) ) );
			
			_batcher.start();
		}

		private function onComplete (event : Event) : void {
			coreInit();
			dispatchEvent( event );
		}

		private var _batcher : Batcher;
		
		private const swfAddressConnector : SWFAddressConnector = new SWFAddressConnector( );

	}
}
